﻿function UserHelper()
{
	function getCurrentUserName()
	{
		return cookies["innovator_user"]; // jshint ignore:line
	}

	function getCurrentIdentity()
	{
		var aml = "<AML> <Item type='User' action='get'><id>{0}</id> <Relationships><Item type='Alias' select='related_id'></Item> </Relationships></Item></AML>".Format(top.aras.getCurrentUserID());
		var userItm = top.aras.newIOMItem();
		userItm.loadAML(aml);
		userItm = userItm.apply();

		var identityItm = userItm.getItemsByXPath("Relationships/Item[@type='Alias']/related_id/Item");
		return identityItm;
	}

	function getOnlineUserIDs()
	{
		return top.aras.applyMethod("VC_GetOnlineUserIds", "");
	}

	function getCookies()
	{
		var cookiesObj = {};
		var all = document.cookie;
		if (all === "")
			throw new Error(SSVC.Utils.GetResource("err_session_expired"));
		var list = all.split("; ");
		for (var i = 0; i < list.length; i++)
		{
			var cookie = list[i];
			var p = cookie.indexOf("=");
			var name = cookie.substring(0, p);
			var value = cookie.substring(p + 1);
			value = decodeURIComponent(value);
			cookiesObj[name] = value;
		}
		return cookiesObj;
	}

	var cookies = getCookies();
	var userHelperInst = this;

	this.OnlineUsers = getOnlineUserIDs();
	this.CurrentUserName = getCurrentUserName();
	this.CurrentUserID = top.aras.getCurrentUserID();
	this.CurrentIdentity = getCurrentIdentity();
	this.UserItems = null;

	this.isOnline = function(userId)
	{
		var index = this.OnlineUsers.indexOf(userId);

		if (index != -1)
			return true;
		else
			return false;
	};

	this.getUsers = function()
	{
		if (this.UserItems == null)
		{
			this.UserItems = top.aras.newIOMItem("User", "get");
			var aml =
				"<AML>" +
					"<Item type='User' action='get' select='first_name, last_name, email, company_name, telephone, picture, logon_enabled'>" +
						"<Relationships>" +
							"<Item type='Alias' action='get' select='id, related_id'>" +
								"<related_id>" +
									"<Item type='Identity' action='get' select='id' />" +
								"</related_id>" +
							"</Item>" +
						"</Relationships>" +
					"</Item>" +
				"</AML>";
			this.UserItems.loadAML(aml);
			this.UserItems = this.UserItems.apply();

			if (this.UserItems.isError())
			{
				this.UserItems = null;
			}
		}
		return this.UserItems;
	};

	this.findUserByIdentityId = function(identityId)
	{
		if (this.UserItems == null)
			this.getUsers();
		var userItm = null;
		if (SSVC.Utils.isNotNullOrUndefined(this.UserItems))
		{
			userItm = this.UserItems.getItemsByXPath("//Relationships/Item[@type='Alias']/related_id/Item[@type='Identity' and @id='{0}']/../../../..".Format(identityId));
			if (userItm.getItemCount() > 1)
			{
				userItm = userItm.getItemByIndex(0);
			}
			else if (userItm.getItemCount() === 0)
			{
				userItm = null;
			}
		}
		return userItm;
	};

	this.getUserInfoByIdentityId = function(identityId)
	{
		var userInfo = new UserInfo();
		userInfo.load(this.findUserByIdentityId(identityId));
		return userInfo;
	};

	this.UpdateOnlineUsers = function()
	{
		this.OnlineUsers = getOnlineUserIDs();
	};

	function UserInfo()
	{
		this.first_name = null;
		this.last_name = null;
		this.company_name = null;
		this.email = null;
		this.telephone = null;
		this.picture = null;
		this.isOnline = null;
		this.logon_enabled = null;

		var UserItem = null;

		this.load = function(userItem)
		{
			UserItem = userItem;
			if (UserItem)
			{
				this.first_name = top.aras.getItemProperty(UserItem.node, "first_name", "");
				this.last_name = top.aras.getItemProperty(UserItem.node, "last_name", "");
				this.company_name = top.aras.getItemProperty(UserItem.node, "company_name", "");
				this.email = top.aras.getItemProperty(UserItem.node, "email", "");
				this.telephone = top.aras.getItemProperty(UserItem.node, "telephone", "");
				this.picture = top.aras.getItemProperty(UserItem.node, "picture", "");
				this.isOnline = userHelperInst.isOnline(top.aras.getItemProperty(UserItem.node, "id", ""));
				this.logon_enabled = top.aras.getItemProperty(UserItem.node, "logon_enabled", "");
			}
		};

		this.loadArr = function(arrUserInfo)
		{
			for (var el in arrUserInfo)
			{
				this.first_name = arrUserInfo[0];
				this.last_name = arrUserInfo[1];
				this.company_name = arrUserInfo[2];
				this.email = arrUserInfo[3];
				this.telephone = arrUserInfo[4];
				this.picture = arrUserInfo[5];
				this.isOnline = arrUserInfo[6];
				this.logon_enabled = arrUserInfo[7];
			}
		};

		this.toArray = function()
		{
			var arr = new Array(8);
			arr[0] = this.first_name;
			arr[1] = this.last_name;
			arr[2] = this.company_name;
			arr[3] = this.email;
			arr[4] = this.telephone;
			arr[5] = this.picture;
			arr[6] = this.isOnline;
			arr[7] = this.logon_enabled;
			return arr;
		};
	}

	UserHelper.newUserInfo = function(userItem)
	{
		var userInfo = new UserInfo();
		userInfo.load(userItem);
		return userInfo;
	};

	UserHelper.newUserInfoFromArr = function(arrUserInfo)
	{
		var userInfo = new UserInfo();
		userInfo.loadArr(arrUserInfo);

		return userInfo;
	};
}
SSVC.Wrap.Dojo.SetObject("SSVC.UserHelper", UserHelper);
